<?php 
	include("/includes/header.php");
	include("/includes/sidebar.php"); 

	$act = $_GET['act'];
	$id = $_GET['id'];

	if ($id != "") {
		$sql_tipe_user = "SELECT * FROM tipe_user WHERE id = $id";
		$query_tipe_user = mysql_query($sql_tipe_user);
		$result_tipe_user = mysql_fetch_array($query_tipe_user);
	}
?>

<div class="header">
	<span>
		<a href="tipe_user.php">
			<i class="fa fa-users"></i> User Type
		</a>
	</span>
	<span>
		<a href="tipe_user-form.php?act=new">
			<i class="fa fa-plus"></i> Add New Type
		</a>
	</span>
</div>

<form class="form-horizontal" data-id="<?php echo $id ?>">
	<div class="content row-fluid"> 
		<div class="span12">

			<input type="hidden" class="act" value="<?php echo $act; ?>">
			<div class="alert_container"></div>

    	<div>
    		<div>
    			<div class="alert alert-info">
    				<h4>
	    				<?php 
	    					if ($act == "edit") {
	    						echo "Edit User Type";
	    					} else {
	    						echo "New User Type";
	    					} 
	    				?>
    				</h4>
			    </div>
					<div class="control-group">
						<label class="control-label">Name</label>
						<div class="controls">
							<input placeholder="Input name" type="text" name="nama" class="nama span6 required" data-toggle="tooltip" title="Input name" required value="<?php echo $result_tipe_user[nama] ?>">
						</div>
						<div id="alert-name" class="controls" style="display:none">
							<div class="alert alert-error span6"><strong>Warning!</strong> Name is already use, try another name.</div>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label">Kode</label>
						<div class="controls">
							<input placeholder="Input code" type="text" name="kode" class="kode span6 required" data-toggle="tooltip" title="Input code" required value="<?php echo $result_tipe_user[kode] ?>">
						</div>
						<div id="alert-code" class="controls" style="display:none">
							<div class="alert alert-error span6"><strong>Warning!</strong> Code is already use, try another code.</div>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label">Notes</label>
						<div class="controls">
							<textarea rows="4" type="text" name="keterangan" class="keterangan span6" data-toggle="tooltip" title="Input notes"><?php echo $result_tipe_user[keterangan] ?></textarea>
						</div>
					</div>
    		</div>
    	</div>
			<div>
				<div class="span12">
					<ul class="breadcrumb well">
						<button type="submit" class="btn btn-info btn-simpan">
							<i class="fa fa-check"></i>
							Save
						</button>
					</ul>
				</div>
			</div>
		</div>
	</div>
</form>

<div class="footer">
</div>

<?php include("/includes/footer.php") ?>       

<SCRIPT TYPE="text/javascript">
	$(document).ready(function() {

		$('.nama').keyup(function(event) {
			validate_name($('form').data('id'), $('.nama').val(), "valid_act", $('.act').val(), "tipe_user");
		});

		$('.kode').keyup(function(event) {
			validate_code($('form').data('id'), $('.kode').val(), "valid_code", $('.act').val(), "tipe_user");
		});

		$('.btn-simpan').click(function(event) {
			event.preventDefault();
			if (validate_form()) {
				if ($('.act').val() == "edit") {
					act_form = "edit";
					act = 2;
					alert_msg = "Data berhasil diubah";
				} else {
					act_form = "new";
					act = 1;
					alert_msg = "Data berhasil ditambah";
				}
				
				var tipeUser = {
					id: $('form').data('id'),
					nama: $('.nama').val(),
					kode: $('.kode').val(),
					keterangan: $('.keterangan').val(),
					act: act,
					act_form: act_form
				}

				$.ajax({
					url: '/action/act_tipe_user.php',
					type: 'POST',
					data: tipeUser,
					cache: false, 
	        success: function(data){
	        	if (data == "sukses") {
		        	alert(alert_msg);
		        	// window.location = "tipe_user.php";
		        	location.reload();
	        	} else {
	        		alert(data);
	        	}
	        }
				})
			}
		});
	});
</SCRIPT>